<!-- panel中方法  Begin-->
// 添加关联产品
function appendRelatedProdcut(pk, encrypt_id, title){
    if($("#pro-" + pk).length){
        $("#pro-" + pk).find("a.name").text(title)
    } else {
        var ids = $("#id_related_prodcuts").val() ? $("#id_related_prodcuts").val().split(",") : [];
        ids.push(pk);
        $("#id_related_prodcuts").val(ids.join(","));

        var html = '<li id="pro-' + pk + '" data-pk="' + pk +'"data-name="'+title+
                '"><a class="name" target="_blank" href="'+location.origin+'/dashboard/city_hunter/product/edit/?product_id=' +
                pk + '/">' + title +
                '</a><button type="button" class="close" data-dismiss="alert">×</button></li>';

        $("#related-product-panel").append(html);
    }
}
// 搜索关联产品
function searchRelatedProduct(){
    var productId = $("#relatedProduct").val(),
        productName = $("#manageRelatedProduct .select2-container .select2-chosen").text();
    if(!productId) return;
    appendRelatedProdcut(productId, null, productName);
}
// 保存分类产品
function saveBtn(){
    $('#saveAddProduct').on('click', function(){
        var cateName = $('#typeName').val(),
            $list = $('#related-product-panel li'),
            $box = $('#addProductTypeList');
        if($list.length < 1){
            alert('请选择关联产品');
            return false;
        }else{
            var ids = [], _li = '', _html = '', flag = false;
            if($(this).hasClass('modify')){ flag = true }
            $.each($list, function(i, j){
                var $j = $(j);
                if(flag){
                    var tag = $('.modify.alert').data('tag');
                    if($('.modify.alert').hasClass('new')){
                        _li += '<li><a target="_blank" class="item" data-id="'+ $j.data('pk') + '" data-name="' + $j.data('name') + '" href="/dashboard/city_hunter/product/edit/?product_id=' + $j.data('pk') + '"><input name="product_list" value="'+cateName+'*'+$j.data('pk')+'" type="hidden">'+$j.data('name')+'</a></li>';
                    }else{
                        _li += '<li><a target="_blank" class="item" data-id="'+ $j.data('pk') + '" data-name="' + $j.data('name') + '" href="/dashboard/city_hunter/product/edit/?product_id=' + $j.data('pk') + '"><input name="product_list" value="'+tag+'_'+$j.data('pk')+'" type="hidden">'+$j.data('name')+'</a></li>';
                    }
                    
                }else{
                    _li += '<li><a target="_blank" class="item" data-id="'+ $j.data('pk') + '" data-name="' + $j.data('name') + '" href="/dashboard/city_hunter/product/edit/?product_id=' + $j.data('pk') + '"><input name="product_list" value="'+cateName+'*'+$j.data('pk')+'" type="hidden">'+$j.data('name')+'</a></li>';
                }
               
            });
            _html = '<div class="alert new alert-success" data-tag="'+cateName+'">' + 
                        '<button type="button" class="close" onclick="javascript:delBtn(this)">×</button>'+
                        '<input type="hidden" name="new_product_type_list" value="'+cateName+'">'+
                        '<h4>'+cateName+'</h4>'+
                        '<ol>'+_li+  
                        '</ol>'+
                        '<a href="#manageRelatedProduct" role="button" class="edit text-right" data-toggle="modal" data-cate="'+cateName+'">编辑</a>'+
                    '</div>';
            if(flag){
                
                $('.alert.modify').find('ol').html(_li);
               
            }else{
                $('#addProductTypeList').append(_html);
                editBtn();
            }
           
            $('#manageRelatedProduct').modal('hide');
        }
    }); 
}
// 重置panel数据
function resetPanel(){
    $('#typeName').val('').removeAttr('disabled');
    $('#related-product-panel').html('');
    $('#saveAddProduct, .alert').removeClass('modify');
}
// 编辑分类产品事件
function editBtn(){
    $('.edit').on('click', function(){
        var $this = $(this),
            $a = $this.parent('.alert').find('.item'),
            cateName = $this.data('cate');
        $('#typeName').val(cateName).attr('disabled', 'disabled');
        $('#related-product-panel').html('');
        $('.alert').removeClass('modify');
        $('#saveAddProduct').addClass('modify');
        $this.parent('.alert').addClass('modify');
        $.each($a, function(i, j){
            var $j = $(j);
            appendRelatedProdcut($j.data('id'), null, $j.data('name'));
        })
    });
}
// 删除分类产品事件
function delBtn(obj){
    var com = confirm("是否删除？");
    if(com){
       $(obj).parent('.alert').remove();
    }
}
<!-- panel中方法 Finish -->
function scroll(){
    window.onscroll = function(){
        var top = document.documentElement.scrollTop || document.body.scrollTop,
            $alert = $('.info-box .alert');
        if(top > 0){
            $alert.removeClass('affix-top').addClass('affix');
        }else{
            $alert.removeClass('affix').addClass('affix-top');
        }
    };
}
// 保存所有信息
function saveAll(){
    var json_data = $('form').serializeObject();
    delete json_data['name']
    json_data.name = $('#realname').val();
    var new_type = [];
    $("input[name='new_product_type_list']").each(function (i, n) {
        new_type.push(n.value);
    });
    json_data["new_product_type_list"] = new_type;

    var old_type = [];
    $("input[name='product_type_list']").each(function (i, n) {
        old_type.push(n.value);
    });
    json_data["product_type_list"] = old_type;

    var pro_list = [];
    $("input[name='product_list']").each(function (i, n) {
        pro_list.push(n.value);
    });
    json_data["product_list"] = pro_list;

    $.ajax({
        url: "/dashboard/city_hunter/edit/",
        type: "POST",
        data: json_data,
        dataType: "json",
        success: function(data) {
            if(data.code == 0){
                window.location.href = '/dashboard/city_hunter/edit/?username=' + $("#username").val();
            }else{
                $('.errorlist').remove();
                var obj = data.msg_dict,
                 _li = '',
                 _html ='';
                for(var i in obj){
                    _li += '<li>'+i+'<br>'+obj[i]+'</li>';
                    $('input[name="'+i+'"]').after('<ul class="errorlist"><li>'+obj[i]+'</li></ul>');
                }
                _html = '<div class="alert alert-error bt-alert affix-top">'+
                            '<button class="close" data-dismiss="alert">×</button>'+
                            '保存失败'+
                            '<ul class="errorlist">'+ _li +'</ul>'+
                            '<br>请检查是否有数据丢失，如有丢失请将完整页面截图发送至baizhaoyang@breadtrip.com'+
                        '</div>';
                $('.info-box').html(_html);
                window.scrollTo(0,0);
                scroll();
            }
        }
    });
}
// 删除所有信息
function delAll(){
    $.ajax({
        url: "/dashboard/city_hunter/edit" + "?hid=" + $("#hunterId").val(),
        type: "DELETE",
        success: function(data) {
            if(data.code == 0){
                window.location.href = '/dashboard/city_hunter/edit/?username=' + $("#username").val();
            }
        }
    });
}
function queryUser(){
    window.location.href = "?username=" + $("#username").val();
}
$(function(){
    // 变更国家
    $("#country").on("change", function(){
        var _this = $(this);
        $.ajax({
            url: "/dashboard/city_hunter/query/province/",
            type: "GET",
            data: {"country": _this.val()},
            success: function(data) {
                var provinceSelOpts = document.getElementById("province");
                provinceSelOpts.length=0;
                var citySelOpts = document.getElementById("city");
                citySelOpts.length=0;
                provinceSelOpts.add(new Option("选择省份", ""));
                citySelOpts.add(new Option("选择城市", ""));
                for(var p_item in data){
                    provinceSelOpts.add(new Option(data[p_item]["fields"]["name_zh"], data[p_item]["pk"]));
                }
            }
        });
    });
    // 变更省份
    $("#province").on("change", function(){
        var _this = $(this);
        $.ajax({
            url: "/dashboard/city_hunter/query/city/",
            type: "GET",
            data: {"province": _this.val()},
            success: function(data) {
                var citySelOpts = document.getElementById("city");
                citySelOpts.length=0;
                citySelOpts.add(new Option("选择城市", ""));
                for(var c_item in data){
                    citySelOpts.add(new Option(data[c_item]["fields"]["name_zh"], data[c_item]["pk"]));
                }
            }
        });
    });
    $('input').on('keydown', function(){
        if(event.keyCode==13){return false;}
    });
    // 选择产品
    $("#relatedProduct").select2({
        placeholder: "--请选择一个相关产品--",
        minimumInputLength: 1,
        ajax: {
            url: "/dashboard/city_hunter/query/trip_product/",
            dataType: 'json',
            data: function (term, page) {
                return {
                    title: term,
                };
            },
            results: function (data, page) {
                var products = [];
                for(var i in data){
                    products.push({
                        'id' : data[i].pk,
                        'text' : data[i].fields.title
                    })

                }
                return {results: products};
            }
        }
    });
    editBtn();
    saveBtn();
});
